package com.ssm.service.dao.impl;

import com.ssm.service.dao.JedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

/**
 * Created by wchen on 2017/5/7.
 */
@Service
//顶部不添加@Service ，此时就要在配置文件applicationContext-jedis.xml中添加该impl的bean
public class JedisClientClusterImpl implements JedisClient {
    public JedisCluster getJedisCluster() {
        return jedisCluster;
    }

    public void setJedisCluster(JedisCluster jedisCluster) {
        this.jedisCluster = jedisCluster;
    }

    @Autowired
    private JedisCluster jedisCluster;


    @Override
    public String get(String key) {
        return jedisCluster.get(key);
    }

    @Override
    public String set(String key, String value) {
        return jedisCluster.set(key, value);
    }

    @Override
    public long incr(String key) {
        return jedisCluster.incr(key);
    }

    @Override
    public Long hset(String hkey, String key, String value) {
        return jedisCluster.hset(hkey, key, value);
    }

    @Override
    public String hget(String hkey, String key) {
        return jedisCluster.hget(hkey, key);
    }

    @Override
    public Long del(String key) {
        return jedisCluster.del(key);
    }

    @Override
    public Long expire(String key, Integer second) {
        return jedisCluster.expire(key, second);
    }

    @Override
    public Long ttl(String key) {
        return jedisCluster.ttl(key);
    }

    @Override
    public Long hlen(String key) {
        return jedisCluster.hlen(key);
    }

    /**
     * 把值插入到链表尾部
     * @param key
     * @param value
     * @return
     */
    @Override
    public Long rpush(String key, String value) {
        return jedisCluster.rpush(key, value);
    }

    @Override
    public boolean exists(String key) {
        return jedisCluster.exists(key);
    }

    /**
     * 计算链表的元素个数
     * @param key
     * @return
     */
    @Override
    public Long llen(String key) {
        return jedisCluster.llen(key);
    }

    /**
     * 返回并删除链表头部元素,因为用户在存储时候是用rpush（先进先出）
     * 先存入的放在链表的第一个，lpop就删除头部的元素并且放回被删除的内容
     * @param key
     * @return
     */
    @Override
    public String lpop(String key) {
        return jedisCluster.lpop(key);
    }

    @Override
    public String lrem(String key, String value) {
        jedisCluster.lrem(key,0,value);
        return null;
    }

    @Override
    public Long hdel(String hkey, String key) {
        return jedisCluster.hdel(hkey,key);
    }
}
